package com.maixy.studyonlineapi.controller;

import com.maixy.studyonlineapi.apis.ApiController;
import com.maixy.studyonlineapi.apis.ApiRequest;
import com.maixy.studyonlineapi.apis.ApiResponse;
import com.maixy.studyonlineapi.apis.Privilege;
import com.maixy.studyonlineapi.bean.Page;
import com.maixy.studyonlineapi.exceptions.MissParamException;
import com.maixy.studyonlineapi.result.PageData;
import com.maixy.studyonlineapi.service.TeamUserService;
import com.maixy.studyonlineapi.utils.DataTypeUtil;
import com.maixy.studyonlineapi.view.TeamUserView;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Classname TeamController
 * @Description TODO
 * @Date 2020/11/6 0:46
 * @Author by maixy
 */
@RestController
@RequestMapping("/v1/team/user")
public class TeamUserController extends ApiController {

    @Resource
    private TeamUserService teamUserService;

    @Privilege
    public ApiResponse getTeamUserList(ApiRequest request) {
        Page page = Page.validation(request.getPage());
        Long teamId = DataTypeUtil.getLong(request.getTeamId());

        PageData<TeamUserView> pageData = teamUserService.getTeamUserList(teamId, page);
        return ApiResponse.ok()
                .addValueToData("pageData", pageData);
    }

    /**
     * 踢出团队成员
     *
     * @param request -- currentUserId 当前用户id
     * @return --
     */
    @Privilege
    public ApiResponse kickOut(ApiRequest request) {
        Long currentUserId = request.getDataParamAsLong("currentUserId");
        Long userId = DataTypeUtil.getLong(request.getUser().getUserId());
        Long teamId = DataTypeUtil.getLong(request.getTeamId());

        teamUserService.kickOut(teamId, userId, currentUserId);
        return ApiResponse.ok();
    }

    /**
     * 主动退出团队
     *
     * @param request -- current
     * @return --
     */
    @Privilege
    public ApiResponse quit(ApiRequest request) {
        Long quitTeamId = request.getDataParamAsLong("quitTeamId");
        Long userId = DataTypeUtil.getLong(request.getUser().getUserId());
        Long teamId = DataTypeUtil.getLong(request.getTeamId());

        if (null == quitTeamId) {
            throw new MissParamException();
        }

        teamUserService.quit(teamId, userId, quitTeamId);
        return ApiResponse.ok();
    }


}
